package com.yunche.yunchebusiness.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseFragment
import com.yunche.yunchebusiness.entity.HistoryData
import com.yunche.yunchebusiness.entity.HomeEntity
import com.yunche.yunchebusiness.mvp.contract.HomeContract
import com.yunche.yunchebusiness.mvp.presenter.HomePresenter
import com.yunche.yunchebusiness.ui.activity.finance.GetMoneyActivity
import kotlinx.android.synthetic.main.fragment_home.*
import lecho.lib.hellocharts.model.*
import java.util.*


/**
 * Create by Crcker on 2018/12/20 11:25
 * Email: Crcker@163.com
 */

@SuppressLint("StaticFieldLeak")

class HomeFragment : BaseFragment(), HomeContract.View {
    val mAxisXValues = ArrayList<AxisValue>()
    var price = 0f

    private val mPresenter by lazy {
        HomePresenter()
    }

    override fun showError(msg: String) {
    }

    override fun setLayout(): Int = R.layout.fragment_home

    companion object {
        private var fragment: HomeFragment? = null
        fun getInstance(): HomeFragment {
            if (fragment == null) {
                fragment = HomeFragment()
            }
            return fragment!!
        }
    }


    override fun getHomeSuccess(homeEntity: HomeEntity) {
        msv_home.showContent()
        if (homeEntity.countData != null) {
            price = homeEntity.countData?.settlement
            tv_home_carNumHandling.text = "${homeEntity.countData?.carNumHandling}"
            tv_home_carNum.text = "${homeEntity.countData.carNum}"
            tv_home_businessVolume.text = "${homeEntity.countData.businessVolume / 10000}"
            tv_home_settlement.text = "${homeEntity.countData.settlement}"
            tv_home_receivables.text = "${homeEntity.countData.receivables}"
        }

        if (homeEntity.historyData != null) {
            tv_home_contact_position.text = homeEntity.tenant.contactPosition
            tv_home_name.text = homeEntity.tenant.name
            initChartData(homeEntity.historyData)
        }

    }

    override fun getHomeError(msg: String) {
        msv_home.showError(msg)
    }

    override fun showLoading() {
        msv_home.showLoading()
        srflay_home.isRefreshing = true
    }

    override fun hideLoading() {
        srflay_home.isRefreshing = false

    }

    override fun initView() {
        mPresenter.attachView(this)
        tv_get_money.setOnClickListener {
            openActivity(GetMoneyActivity::class.java)
        }

        srflay_home.setOnRefreshListener {
            initData()
        }

        msv_home.setOnRetryClickListener {
            initData()
        }

    }

    private fun initChartData(historyData: List<HistoryData>) {
        mAxisXValues.clear()
        val columns = ArrayList<Column>()
        var values: MutableList<SubcolumnValue>
        var chartData: ColumnChartData? = null

        for ((index, value) in historyData.asReversed().withIndex()) {

            values = ArrayList()
            values.add(SubcolumnValue(value.price / 10000, Color.parseColor("#FFA21F")))
            val column = Column(values)
            column.setHasLabels(true)
            columns.add(column)
            mAxisXValues.add(AxisValue(index.toFloat()).setLabel(value.time))
        }


        chartData = ColumnChartData(columns)
        chartData.valueLabelBackgroundColor = Color.WHITE
        chartData?.fillRatio = 0.4f
        val axisX = Axis()
        val axisY = Axis().setHasLines(true)


        axisX.setHasLines(false)
        axisX.values = mAxisXValues

        chartData?.axisXBottom = axisX
        chartData?.axisYLeft = axisY

        column_chart.isZoomEnabled = false
        column_chart.startDataAnimation()
        column_chart.columnChartData = chartData
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initData()
        }
    }

    override fun initData() {
        mPresenter.getHomePage()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }
}